using AutoMapper;
using MediatR;
using ThreeTeams.API.Application.Command.BoxTypeManageMentCommand;
using ThreeTeams.Domain;
using ThreeTeams.ErrorCode;
using ThreeTeams.Infrastructure;

namespace ThreeTeams.API.Application.CommandHandler.BoxTypeManageMentCommandHandler
{
    public class CreateBoxTypeCommandHandler : IRequestHandler<CreateBoxTypeCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<BoxType> baseRepository;
        private readonly IMapper mapper;

        public CreateBoxTypeCommandHandler(IBaseRepository<BoxType> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(CreateBoxTypeCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            var entity = mapper.Map<BoxType>(request);
            var res = await baseRepository.Create(entity);
            if (res <= 0)
            {
                result.Code = ApiEnum.Fail;
                result.Message = "添加失败";
                return await Task.FromResult(result);
            }
            result.Code = ApiEnum.Ok;
            result.Message = "添加成功";
            return await Task.FromResult(result);
        }
    }
} 